/* @(#)GSAuditAndSubmitToApepAction.java   
 *
 * Project: GS
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   Jenny Li    2006-4-4     新增
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
 *        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
 *        Haidian District, Beijing ,100081, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with HiTRUST.
 *
 * Warning:
 * =============================================================================
 * 
 */
package com.hitrust.gs.action;

import java.util.ArrayList;

import system.LoginContext;

import com.hitrust.gs.bean.GSManufactureBean;
import com.hitrust.gs.business.GSBusOrderProcesser;
import com.hitrust.gs.business.GSManufactureProcessor;
import com.hitrust.gs.dao.GSManufactureDAO;
import com.hitrust.gs.form.GSOrderAuthorizeSaveForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.vo.GSOrderAuthorizeSaveVO;

/**
 * 
 */
public class GSAuditAndSubmitToApepAction extends GSBaseAction {

    public String execute(GSBaseForm aForm) throws Exception {
        
        LoginContext tOperatorBean = (LoginContext)aForm.getSession().getAttribute(GSBaseConstants.LOGIN);
        //取得页面参数
        GSOrderAuthorizeSaveForm tForm = (GSOrderAuthorizeSaveForm)aForm;
        String[] detailCheckbox = tForm.getIndexs();
        String[] detailId = tForm.getDetailId();
        String[] orderCount = tForm.getOrderCount();
        String[] oldOrderCount = tForm.getOldOrderCount();
        String[] providerId = tForm.getNewProviderId();
        String[] oldProviderId = tForm.getOldProviderId();
        String[] unitPrice = tForm.getUnitPrice();
        String[] manufactureId = tForm.getManufactureId();
        String[] oldManufactureId = tForm.getOldManufactureId();
        String[] orderId = tForm.getOrderId();
        String[] isTradeFlatManuf = tForm.getIsTradeFlatManuf();
        String[] isTradeFlatProvider = tForm.getIsTradeFlatProvider();
        String[] oldUnitPrice = tForm.getOldUnitPrice();
        String[] oldmode=tForm.getOldmode();
        String[] mode=tForm.getMode();
		// add by Jackey start
		//增加对送货日期和送货时间的设置
		String[] deliverDate = tForm.getDeliverDate();
		String[] deliverTime = tForm.getDeliverTime();
		// add by Jackey end       
       
        ArrayList tList = new ArrayList();
        
        GSManufactureDAO mDAO = new GSManufactureDAO();
        GSManufactureProcessor mp = new GSManufactureProcessor(); 
        for(int i=0;i<detailCheckbox.length;i++){
            for(int j=0;j<detailId.length;j++){
                if(detailId[j].equals(detailCheckbox[i])){
                    GSOrderAuthorizeSaveVO vo = new GSOrderAuthorizeSaveVO ();
                    vo.detailId = detailId[j];
                    
                    vo.orderCount = orderCount[j];
                    vo.unitPrice = unitPrice[j];
                    vo.providerId = providerId[j];
                    vo.orderId = orderId[j];
                    vo.isTradeFlatManuf = isTradeFlatManuf[j];
                    vo.isTradeFlatProvider = isTradeFlatProvider[j];
                    vo.mode=mode[j];
                    //记录原始值
                    vo.oldManufactureId = oldManufactureId[j];
                    vo.oldOrderCount = oldOrderCount[j];
                    vo.oldProviderId = oldProviderId[j];
                    vo.oldUnitPrice = oldUnitPrice[j];
                    vo.oldmode=oldmode[j];
					//记录送货日期和送货时间
					vo.deliverDate = deliverDate[j];
					vo.deliverTime = deliverTime[j];                    
                    
                    //验证传入的供应商id和产品编码是否吻合
                    //如果不吻合，根据产品编码查询集中编码，并根据集中编码和传入的供应商id定位正确的产品编码
                    GSManufactureBean tmBean = mDAO.getManufacture(manufactureId[j]);
                    String check_concentricityId = tmBean.getGs_concentricity();
                    String check_providerId = tmBean.getGs_provider_id();
                    if(!check_providerId.equals(providerId[j])){
                    	vo.manufactureId = mp.getManufactureIdByConcentricityIdAndProviderId(check_concentricityId,providerId[j]);
                    }else{
                    	vo.manufactureId = manufactureId[j];                    	
                    }
                    
                    tList.add(vo);
                }
            }
        }
        GSBusOrderProcesser tProc = new GSBusOrderProcesser();
        //保存修改审核通过
        tProc.authorizeOrder(tOperatorBean,tList);
        //执行提交
        try {
            String msg= tProc.orderSubmitNewToApep(tOperatorBean,tList,null);
            tForm.getRequest().setAttribute("message",msg);
        } catch (Exception e) {
            GSLogWriter.error(e.getMessage());
            aForm.getRequest().setAttribute("message", "订单提交失败！");
            aForm.getRequest().setAttribute("action", "A150201.do");
            throw new GSException(e.getMessage());
        }
        //隐藏是否重新查询参数
        tForm.getRequest().setAttribute("ISQUERYCONDITIONEXIST","T");
        
        //取得产品分类
        GSManufactureProcessor tManufProc = new GSManufactureProcessor();
        tManufProc.getManufactureOfLevel(aForm);

        return GSBaseConstants.FORWARD_SUCCESS;
    }

}
